package com.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

public class TankFrame extends Frame {
    Tank myTank = new Tank(200, 300, Dir.DOWN, Group.GOOD, this);
    ArrayList<Bullet> bullets = new ArrayList<>();
    ArrayList<Tank> tanks = new ArrayList<>();
    ArrayList<Explode> explodes = new ArrayList<>();

    Bullet b = new Bullet(300, 300, Dir.DOWN, myTank,this);
    public static final int GAME_WIDTH = 800, GAME_HEIGHT = 600;

    public  TankFrame(){
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setResizable(false);
        setTitle("tank war");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setVisible(true);

        this.addKeyListener(new MyKeyListener());
    }

    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g){
        Color c = g.getColor();
        g.setColor(Color.white);
        g.drawString("bulletCount:" + bullets.size(), 30, 50);
        g.drawString("TankCount:" + tanks.size(), 30, 70);
        g.setColor(c);

        myTank.Paint(g);

        //坦克描画
        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).Paint(g);
        }

        //字段描画;
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).Paint(g);
        }
        //爆炸描画
        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).Paint(g);
        }
        //碰撞检测
        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collidwith(tanks.get(j));
            }

        }
    }

    class  MyKeyListener extends KeyAdapter{

        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        private void setDirByKey(int key, boolean B){
            switch (key){
                case KeyEvent.VK_LEFT:
                    bL = B;
                    break;
                case KeyEvent.VK_UP:
                    bU = B;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = B;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = B;
                    break;
                default:
                    break;
            }
            setMainTanDir();
        }

        private void setMainTanDir() {
            if (!bL && !bU && !bR && !bD) myTank.setMoving(false);
            else {
                myTank.setMoving(true);
                if (bL) myTank.setDir(Dir.LEFT);
                if (bU) myTank.setDir(Dir.UP);
                if (bR) myTank.setDir(Dir.RIGHT);
                if (bD) myTank.setDir(Dir.DOWN);
            }

        }
        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            setDirByKey(key, true);

        }



        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();

            setDirByKey(key, false);

            switch (key){
                case KeyEvent.VK_CONTROL: myTank.fire();
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {
            super.keyTyped(e);
        }
    }

}
